import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RefreshTableView extends StatefulWidget {
  final Function onRefresh;
  final Function onLoadMore;
  final IndexedWidgetBuilder itemBuilder;
  final int itemCount;
  final double height;

  const RefreshTableView({
    Key key,
    this.onRefresh,
    this.onLoadMore,
    @required this.itemBuilder,
    @required this.itemCount, this.height = 100.0,
  }) : super(key: key);

  @override
  _RefreshTableViewState createState() => _RefreshTableViewState();
}

class _RefreshTableViewState extends State<RefreshTableView> {
  ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        if (widget.onLoadMore != null&&widget.itemCount>0) {
          widget.onLoadMore();
        }
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _scrollController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      child: widget.itemCount>0? _dataTableViewWidget():_noDataTableViewWidget(),
      onRefresh: () async {
        if (widget.onRefresh != null) {
          widget.onRefresh();
        }
      },
    );
  }
  Widget _dataTableViewWidget(){
    return ListView.builder(
      itemBuilder: widget.itemBuilder,
      itemCount: widget.itemCount,
      controller: _scrollController,
    );
  }
  Widget _noDataTableViewWidget(){
    var height = MediaQuery.of(context).padding;
    print("height =$height");
    return PageView(
      children: <Widget>[
        Container(
          child: Image.asset("assets/images/blank.png",width: 100,height: 100,),
        ),
      ],
    );
  }

}
